<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('上传附件到学习培训')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-study-edit">
            <input name="studyId" th:value="${studyId}" type="hidden">
            <input name="modelName" th:value="${model}" type="hidden">
           <div class="form-group">
                <label class="col-sm-3 control-label">上传文件：</label>
                <div class="col-sm-8">
                    <input id="filePath" name="filePath"type="file" multiple>
                    <!--<input name="filePath" class="form-control" type="text">-->
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:inline="javascript">
        var prefix = ctx + "company/study"
        $("#form-study-add").validate({
            focusCleanup: true
        });
        $(document).ready(function () {
            $("#filePath").fileinput({
                'theme': 'explorer-fas',
                overwriteInitial: false,
                initialPreviewAsData: true,
                language: 'zh', //设置语言
                //uploadUrl: prefix + "/add", //上传的地址
                allowedFileExtensions: ['bmp', 'gif', 'jpg', 'jpeg', 'png', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'html', 'htm', 'txt', 'rar', 'zip', 'gz', 'bz2', 'mp4', 'avi', 'rmvb', 'pdf'],//接收的文件后缀
                maxFilesNum : 5,//上传最大的文件数量
                //uploadExtraData:{"id": 1, "fileName":'123.mp3'},
                uploadAsync: true, //默认异步上传
                showUpload: false, //是否显示上传按钮
                showRemove : true, //显示移除按钮
                showPreview : true, //是否显示预览
                showCaption: true,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                //dropZoneEnabled: true,//是否显示拖拽区域
                maxFileSize: 0,//单位为kb，如果为0表示不限制文件大小
               //minFileCount: 0,
               //maxFileCount: 10, //表示允许同时上传的最大文件个数
                enctype: 'multipart/form-data',
                validateInitialCount:true,
                //previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                //msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
            })

        });

        /*function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-study-add').serialize());
            }
        }*/

        $("input[name='invalidTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

       function submitHandler() {
            if ($.validate.form()) {
                uploadFile();
            }
        }

        function uploadFile() {
            var formData = new FormData();
            if ($('#filePath')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            for (var i=0;i<$('#filePath')[0].files.length;i++) {
                formData.append('studyId', $("input[name='studyId']").val());
                formData.append('modelName', $("input[name='modelName']").val());
                formData.append('file', $('#filePath')[0].files[i]);
                $.ajax({
                    url: prefix + "/UploadAdd",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
                formData = new FormData();
            }
           // $.modal.alertWarning("上传成功");
        }
    </script>
</body>
</html>